package com.shop.loan.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Encryption {

    public static String md5Encrypt(String input) {
        try {
            // 创建一个 MD5 哈希对象
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 更新哈希对象以包含输入字符串的字节
            byte[] messageDigest = md.digest(input.getBytes());

            // 将哈希值转换为16进制格式
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }

            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
